package pers.w.chaptertalk.funs;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;

import pers.w.chaptertalk.R;

public abstract class BookThread extends Thread implements Runnable {

    protected void handle_tianlai(ArrayList<BookInfo> books, Document doc) {
        Elements result_list =        doc.select("div.result-game-item");

        for (int i = 0; i < result_list.size(); i++) {
            //  Log.d("SEARCH",result_list.get(i).select("h3").text() + "\n");
            BookInfo book = new BookInfo();
            Elements tmpElements;
            Element curBookElement = result_list.get(i);
            tmpElements = curBookElement.select("h3");
            book.setFieldValue(R.string.info_name, tmpElements.get(0).text());
            book.setFieldValue(R.string.info_uri, IReader.天籁 + tmpElements.select("a").get(0).attr("href"));


            book.setFieldValue(R.string.info_img, curBookElement.select("img").attr("src"));
            book.setFieldValue(R.string.info_introduction, curBookElement.select("p.result-game-item-desc").text());
            tmpElements = curBookElement.select("p.result-game-item-info-tag");

            book.setFieldValue(R.string.info_author, tmpElements.get(0).select("span").get(1).text());
            book.setFieldValue(R.string.info_type, tmpElements.get(1).select("span").get(1).text());
            book.setFieldValue(R.string.info_lastUpdateTime, tmpElements.get(2).select("span").get(1).text());
            book.setFieldValue(R.string.info_lastChapterName, tmpElements.get(3).select("a.result-game-item-info-tag-item").text());

            books.add(book);
        }
    }

    public final static int BQG1 =1;
    public final static int BQG2 =2;
    public final static int BQG3 =3;
    public final static int BQG4 =4;
    public final static int TIANLAI =11;

    protected void handle_bqg(ArrayList<BookInfo> books, Document doc, String divClass, int start_li) {
        Elements result_list = doc.select(divClass);
        if (result_list.size() > 0) {
            result_list = result_list.get(0).select("li");
            for (int i = start_li; i < (result_list.size()); i++) {

                BookInfo book = new BookInfo();
                Element curBookElement = result_list.get(i);
                Elements tmpElements = curBookElement.select("span");//得到当前书籍详细信息
                //第二个sapn是书名和地址.在子节点a中
                Element tmpElement = tmpElements.get(1).select("a").get(0);
                book.setFieldValue(R.string.info_name, tmpElement.text());
                book.setFieldValue(R.string.info_uri, tmpElement.attr("href"));

                book.setFieldValue(R.string.info_img, "");
                book.setFieldValue(R.string.info_introduction, "");

                book.setFieldValue(R.string.info_author, tmpElements.get(3).text());
                book.setFieldValue(R.string.info_type, tmpElements.get(0).text());
                //第三个节点是最后更新信息
                tmpElement = tmpElements.get(2).select("a").get(0);
                book.setFieldValue(R.string.info_lastChapterName, tmpElement.text());

                tmpElement = tmpElements.get(4);
                book.setFieldValue(R.string.info_lastUpdateTime, tmpElement.text());
                books.add(book);
            }
        }
    }
            /*private void handle_bqg1(ArrayList<BookInfo> books, Document doc) {
                Elements result_list = doc.select("div.novelslistss");
                result_list = result_list.get(0).select("li");
                for (int i = 0; i < result_list.size(); i++) {

                    BookInfo book = new BookInfo();
                    Element curBookElement = result_list.get(i);
                    Elements tmpElements = curBookElement.select("span");//得到当前书籍详细信息
                    //第二个sapn是书名和地址.在子节点a中
                    Element tmpElement = tmpElements.get(1).select("a").get(0);
                    book.setFieldValue(R.string.info_name, tmpElement.text());
                    book.setFieldValue(R.string.info_uri, tmpElement.attr("href"));

                    book.setFieldValue(R.string.info_img, "");
                    book.setFieldValue(R.string.info_introduction, "");

                    book.setFieldValue(R.string.info_author, tmpElements.get(3).text());
                    book.setFieldValue(R.string.info_type, tmpElements.get(0).text());
                    //第三个节点是最后更新信息
                    tmpElement = tmpElements.get(2).select("a").get(0);
                    book.setFieldValue(R.string.info_lastChapterName, tmpElement.text());

                    tmpElement = tmpElements.get(4);
                    book.setFieldValue(R.string.info_lastUpdateTime, tmpElement.text());
                    books.add(book);
                }
            }*/

}
